package com.itcast.controller;


import com.alipay.api.AlipayApiException;
import com.itcast.common.res.ResultData;
import com.itcast.pojo.dto.OrderDTO;
import com.itcast.service.Ordersservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.bind.ValidationException;
import java.io.UnsupportedEncodingException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-19
 */
@Controller
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private Ordersservice ordersservice;

    @PostMapping("/insert")
    @ResponseBody
    public String insertOrder(@RequestBody OrderDTO dto) throws UnsupportedEncodingException, AlipayApiException {
        return ordersservice.insertOrder(dto);
    }

    @PostMapping("/getOrderInfo")
    @ResponseBody
    public ResultData getOrderInfo(String userId){
        ResultData rc=new ResultData();
        try {
            rc.setData(ordersservice.getOrderInfo(userId));
        }catch (ValidationException e){
            rc.setError();
        } catch (Exception e){
            e.printStackTrace();
        }
        return rc;
    }
}

